package com.easydisk.user.controller;

import com.easydisk.common.util.UserIdUtil;
import com.easydisk.constant.FileConstant;
import com.easydisk.response.ApiResponse;
import com.easydisk.response.R;
import com.easydisk.user.ShareUserInfoVO;
import com.easydisk.user.annotation.RateLimiter;
import com.easydisk.user.feignclient.FileFeignClient;
import com.easydisk.user.model.entity.User;
import com.easydisk.user.model.req.*;
import com.easydisk.user.model.vo.UserInfo;
import com.easydisk.user.service.LoginService;
import com.easydisk.user.service.UserService;
import com.easydisk.vo.UserFileParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Autowired
    private UserService userService;


    /**
     * 返回当前登录的用户信息和根文件夹
     * @return
     */
    @GetMapping("")
    public R getUserInfo() {
        Long userId = UserIdUtil.get();
        UserInfo userInfo = new UserInfo();
        UserFileParam data = fileFeignClient.getUserFileParam(FileConstant.ROOT_FOLDER_PARENT_ID, userId).getData();
        BeanUtils.copyProperties(data, userInfo);
        User byId = userService.getById(userId);
        userInfo.setUsername(byId.getUsername());
        return R.data(userInfo);
    }

    /**
     * 获取分享的用户信息
     *
     */
    @GetMapping("/get/share/info")
    public ApiResponse<ShareUserInfoVO> getShareUserInfo(@RequestParam Long userId) {
        User byId = userService.getById(userId);
        ShareUserInfoVO shareUserInfoVO = new ShareUserInfoVO();
        shareUserInfoVO.setUserId(String.valueOf(byId.getId()));
        shareUserInfoVO.setUsername(byId.getUsername());
        return ApiResponse.data(shareUserInfoVO);
    }


    /**
     * 用户登录，返回token
     */
    @ApiOperation("账号密码登录")
    @PostMapping("/login")
    public R login(@Validated @RequestBody LoginReq loginReq) throws Exception {
        String username = loginReq.getUsername();
        String password = loginReq.getPassword();
        String token =  userService.login(username, password);
        return R.data(token);
    }


    /**
     * 退出登录，通过IDutil上下文获取当前用户id
     */
    @ApiOperation("退出登录")
    @PostMapping("/exit")
    public R logout () {
        return loginService.loginout();
    }


    /**
     * 用户注册
     */
    @ApiOperation(
            value = "用户注册",
            notes = "该接口提供了用户注册的功能")
    @PostMapping("/register")

    public R register(@Validated @RequestBody RegisterReq registerReq) {
        userService.register(registerReq);
        return R.success();
    }


    /**
     * 查看是否有该用户邮箱,成功后发送邮箱验证码
     */
    @PostMapping("/email/check/{email}")
    public R<String>  checkEmail(@PathVariable String email)  {
        userService.sendEmailCode(email);
        return R.data(email);
    }

    /**
     *  邮箱验证码校验
     */
    @PostMapping("/email/code/check")
    public R<String>  checkEmailCode(@Validated @RequestBody CheckCodeReq checkCodeReq)  {
        String token = userService.checkEmailCode(checkCodeReq.getEmail(), checkCodeReq.getCode());
        return R.data(token);
    }

    /**
     * 邮箱修改密码
     * @param resetPasswordReq
     * @return
     */
    @PostMapping("/email/reset/password")
    public R<String> resetPasswordByEmail(@Validated @RequestBody ResetPasswordReq resetPasswordReq) {
        userService.resetPasswordByEmail(resetPasswordReq.getEmail(),
                resetPasswordReq.getToken(), resetPasswordReq.getNewPassword());
        return R.success();
    }


    /**
     * 修改密码
     */
    @ApiOperation("修改密码")
    @PostMapping("/password/change")
    public R  changePassword(@Validated @RequestBody UpdatePasswordReq passwordReq){

        return  loginService.changePassword(passwordReq.getNewPassword(),passwordReq.getPassword());
    }
}
